<script setup lang="ts">
  import { onShow } from '@dcloudio/uni-app'
  import { useGuessList } from '@/composables'
  import { useMemberStore } from '@/stores'
  import { getinfo } from '@/services/home'
  import { gokefu } from '@/utils/index'
  import { ref } from 'vue'
  // 获取屏幕边界到安全区域距离
  const { safeAreaInsets } = uni.getSystemInfoSync()
  // 订单选项
  // 获取会员信息
  const memberStore = useMemberStore()

  const accountinfo = ref({
    balance: '',
    mobile: '',
    nickName: '',
  })
  // 页面加载
  onShow(async () => {
    if(memberStore.profile){
      const res = await getinfo()
      if (res.success) {
        accountinfo.value = res.data
      }else{
        memberStore.clearProfile()
        uni.showModal({
          confirmText:'去登录',
          title: '提示',
          content: res.msg + '请刷新或重新登录',
          success: async function (result) {
            if (result.confirm) {
              uni.navigateTo({ url: '/pages/login/login?redirect='+ route +'&params=' + JSON.stringify(options) })
            }
          },
        })
      }
    }else{
      accountinfo.value = {
        balance: '',
        mobile: '',
        nickName: '',
      }
    }
  })
  const gowithdrawal = (url:any) => {
    if(memberStore.profile){
      uni.navigateTo({
        url: url,
      })
    }else{
      const currentPage = getCurrentPages().pop()
      const options = currentPage?.$page.options
      const route = currentPage?.route
      uni.showModal({
        confirmText:'去登录',
        title: '提示',
        content: '您的登录状态已过期，请重新登录？',
        success: async function (result) {
          if (result.confirm) {
            uni.navigateTo({ url: '/pages/login/login?redirect='+ route +'&params=' + JSON.stringify(options) })
          }
        },
      })
    }
  }
  const { guessRef, onScrolltolower } = useGuessList()
</script>

<template>
  <scroll-view enable-back-to-top @scrolltolower="onScrolltolower" class="viewport">
    <!-- 个人资料 -->
    <view class="profile" :style="{ paddingTop: safeAreaInsets!.top + 'px' }">
      <!-- 情况1：已登录 -->
      <view class="overview" v-if="memberStore.profile">
        <navigator url="/pagesMember/profile/profile" hover-class="n">
          <image class="avatar" :src="memberStore.profile.avatar" mode="aspectFill"></image>
        </navigator>
        <view class="meta">
          <view class="nickname"> 昵称：{{ accountinfo.nickName }} </view>
          <view> 手机号：{{ accountinfo.mobile }} </view>
        </view>
      </view>
      <!-- 情况2：未登录 -->
      <view class="overview" v-else>
        <navigator url="/pages/login/login" hover-class="none">
          <image class="avatar gray" mode="aspectFill"
            src="https://yjy-xiaotuxian-dev.oss-cn-beijing.aliyuncs.com/picture/2021-04-06/db628d42-88a7-46e7-abb8-659448c33081.png">
          </image>
        </navigator>
        <view class="meta">
          <navigator url="/pages/login/login" hover-class="none" class="nickname">
            未登录
          </navigator>
          <view class="extra">
            <text class="tips">点击登录账号</text>
          </view>
        </view>
      </view>
      <view class="settings" url="/pagesMember/settings/settings" hover-class="none">
        设置
      </view>
      <view class="waller">
        <text>钱包余额：{{ accountinfo.balance }}</text>
        <button class="button" @tap="gowithdrawal('/pagesMember/withdrawal/withdrawal')">申请提现</button>
      </view>
      <view class="nalist">
        <view class="nabox arrow" @tap="gowithdrawal('/pagesMember/questrecord/questrecord')">任务记录</view>
        <view class="nabox arrow" @tap="gowithdrawal('/pagesMember/shouzhirecord/shouzhirecord')">收支记录</view>
        <view class="nabox arrow" @tap="gokefu">咨询客服</view>
        <view class="nabox arrow" @tap="gowithdrawal('/pagesMember/settings/settings')">设置</view>
      </view>
    </view>
  </scroll-view>
</template>

<style lang="scss">
  page {
    height: 100%;
    overflow: hidden;
    background-color: #f7f7f8;
  }

  .button {
    margin: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 180rpx;
    height: 56rpx;
    font-size: 28rpx;
    border-radius: 72rpx;
    color: #fff;
    background-color: #1e9fff;
  }

  .viewport {
    height: 100%;
    background-repeat: no-repeat;
    background-image: url(@/static/images/center_bg.jpg);
    background-size: 100% auto;
  }

  .waller {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 40rpx;
    background-color: #fff;
    margin: 60rpx 20rpx;
    border-radius: 20rpx;
  }

  .nalist {
    background-color: #fff;
    margin: 60rpx 20rpx;
    margin-top: 120rpx;

    .nabox {
      padding: 30rpx;
      border-bottom: 1px solid #eee;
      position: relative;
    }

    .arrow::after {
      content: '\e6c2';
      position: absolute;
      top: 50%;
      color: #ccc;
      font-family: 'erabbit' !important;
      font-size: 32rpx;
      transform: translateY(-50%);
      right: 40rpx;
    }
  }

  /* 用户信息 */
  .profile {
    margin-top: 30rpx;
    position: relative;

    .overview {
      display: flex;
      height: 120rpx;
      padding: 0 36rpx;
      color: #fff;
    }

    .avatar {
      width: 120rpx;
      height: 120rpx;
      border-radius: 50%;
      background-color: #eee;
    }

    .gray {
      filter: grayscale(100%);
    }

    .meta {
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: flex-start;
      line-height: 30rpx;
      padding: 16rpx 0;
      margin-left: 20rpx;
    }

    .nickname {
      margin-bottom: 16rpx;
      font-size: 30rpx;

      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }

    .extra {
      display: flex;
      font-size: 20rpx;
    }

    .tips {
      font-size: 22rpx;
    }

    .update {
      padding: 3rpx 10rpx 1rpx;
      color: rgba(255, 255, 255, 0.8);
      border: 1rpx solid rgba(255, 255, 255, 0.8);
      margin-right: 10rpx;
      border-radius: 30rpx;
    }

    .settings {
      position: absolute;
      bottom: 0;
      right: 40rpx;
      font-size: 30rpx;
      color: #fff;
    }
  }

  /* 我的订单 */
  .orders {
    position: relative;
    z-index: 99;
    padding: 30rpx;
    margin: 50rpx 20rpx 0;
    background-color: #fff;
    border-radius: 10rpx;
    box-shadow: 0 4rpx 6rpx rgba(240, 240, 240, 0.6);

    .title {
      height: 40rpx;
      line-height: 40rpx;
      font-size: 28rpx;
      color: #1e1e1e;

      .navigator {
        font-size: 24rpx;
        color: #939393;
        float: right;
      }
    }

    .section {
      width: 100%;
      display: flex;
      justify-content: space-between;
      padding: 40rpx 20rpx 10rpx;

      .navigator,
      .contact {
        text-align: center;
        font-size: 24rpx;
        color: #333;

        &::before {
          display: block;
          font-size: 60rpx;
          color: #ff9545;
        }

        &::after {
          border: none;
        }
      }

      .contact {
        padding: 0;
        margin: 0;
        border: 0;
        background-color: transparent;
        line-height: inherit;
      }
    }
  }

  /* 猜你喜欢 */
  .guess {
    background-color: #f7f7f8;
    margin-top: 20rpx;
  }
</style>
